﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Public
{
	public partial class PipeShowSetForm : System.Windows.Forms.Form
	{
		public PipeShowSetForm(string filePath, Document doc)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.filePath = filePath;
			this.doc = doc;
		}

		private void PipeShowSetForm_Load(object sender, EventArgs e)
		{
			this.btnAdd.Image = Resources.AddNew16;
			this.btnDel.Image = Resources.delete16;
			this.dataGridView1.AllowUserToAddRows = false;
			this.BindData();
			this.chkDiameter.Enabled = this.chkElectric.Checked;
			this.hyTxtDiameter.Enabled = this.chkElectric.Checked;
		}

		private void BindData()
		{
			this.ds = new DataSet();
			this.ds.ReadXml(this.filePath);
			this.dataGridView1.DataSource = this.ds.Tables["elevation"];
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = this.dataGridView1.Columns[0] as DataGridViewTextBoxColumn;
			dataGridViewTextBoxColumn.HeaderText = "起点标高(mm)";
			dataGridViewTextBoxColumn.Width = 60;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn.MinimumWidth = 60;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = this.dataGridView1.Columns[1] as DataGridViewTextBoxColumn;
			dataGridViewTextBoxColumn2.HeaderText = "终点标高(mm)";
			dataGridViewTextBoxColumn2.Width = 60;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn2.MinimumWidth = 60;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = this.dataGridView1.Columns[2] as DataGridViewTextBoxColumn;
			dataGridViewTextBoxColumn3.HeaderText = "构件颜色";
			dataGridViewTextBoxColumn3.Width = 125;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn3.MinimumWidth = 125;
			dataGridViewTextBoxColumn3.ReadOnly = true;
			this.ColorInint();
			this.ReadUserSetting();
		}

		private void ColorInint()
		{
			int count = this.dataGridView1.Rows.Count;
			for (int i = 0; i < count; i++)
			{
				if (this.dataGridView1[2, i].Value != null)
				{
					string rgb = this.dataGridView1[2, i].Value.ToString();
                    System.Drawing.Color xmldataColor = PipeShowHelper.getXMLDataColor(rgb);
					this.dataGridView1[2, i].Style.ForeColor = xmldataColor;
					this.dataGridView1[2, i].ReadOnly = true;
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.isElectric = this.chkElectric.Checked;
			if (this.chkDiameter.Checked)
			{
				if (this.hyTxtDiameter.Text != "")
				{
					this.dDiameter = double.Parse(this.hyTxtDiameter.Text);
				}
			}
			else
			{
				this.dDiameter = 0.0;
			}
			if (!this.CheckElevation())
			{
				return;
			}
			this.ds.WriteXml(this.filePath);
			this.ds.Dispose();
			this.WriteUserSetting();
			base.DialogResult = DialogResult.Abort;
		}

		private bool CheckElevation()
		{
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (double.Parse(this.dataGridView1[0, i].Value.ToString()) >= double.Parse(this.dataGridView1[1, i].Value.ToString()))
				{
					YJKMessageBox.Show("起点标高应小于终点标高，请检查。");
					return false;
				}
			}
			if (this.dataGridView1.Rows.Count > 1)
			{
				for (int j = 0; j < this.dataGridView1.Rows.Count - 1; j++)
				{
					for (int k = 0; k < j + 1; k++)
					{
						double num = double.Parse(this.dataGridView1[0, k].Value.ToString());
						double num2 = double.Parse(this.dataGridView1[1, k].Value.ToString());
						if ((num <= double.Parse(this.dataGridView1[0, j + 1].Value.ToString()) && double.Parse(this.dataGridView1[0, j + 1].Value.ToString()) < num2) || (num < double.Parse(this.dataGridView1[1, j + 1].Value.ToString()) && double.Parse(this.dataGridView1[1, j + 1].Value.ToString()) <= num2))
						{
							YJKMessageBox.Show("标高区间有重叠，请检查。");
							return false;
						}
					}
				}
				for (int l = 0; l < this.dataGridView1.Rows.Count - 1; l++)
				{
					for (int m = 0; m < l + 1; m++)
					{
						double num3 = double.Parse(this.dataGridView1[0, l + 1].Value.ToString());
						double num4 = double.Parse(this.dataGridView1[1, l + 1].Value.ToString());
						if ((num3 <= double.Parse(this.dataGridView1[0, m].Value.ToString()) && double.Parse(this.dataGridView1[0, m].Value.ToString()) < num4) || (num3 < double.Parse(this.dataGridView1[1, m].Value.ToString()) && double.Parse(this.dataGridView1[1, m].Value.ToString()) <= num4))
						{
							YJKMessageBox.Show("标高区间有重叠，请检查。");
							return false;
						}
					}
				}
			}
			return true;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			this.ds.WriteXml(this.filePath);
			NewPipeShowForm newPipeShowForm = new NewPipeShowForm(this.filePath);
			newPipeShowForm.ShowDialog();
			if (newPipeShowForm.DialogResult == DialogResult.OK)
			{
				this.BindData();
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			this.ds.WriteXml(this.filePath);
			if (this.dataGridView1.Rows.Count == 1)
			{
				YJKMessageBox.Show("不能删除最后一个标高区间。");
				return;
			}
			DialogResult dialogResult = YJKMessageBox.Show("确定要删除吗？", MessageBoxButtons.YesNo);
			if (dialogResult == DialogResult.Yes)
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.filePath);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("elevationSet");
				XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("elevation");
				if (this.dataGridView1.CurrentRow != null && xmlNode != null)
				{
					xmlNode.RemoveChild(elementsByTagName[this.dataGridView1.CurrentRow.Index]);
				}
				double num = double.Parse(this.dataGridView1[0, this.dataGridView1.CurrentRow.Index].Value.ToString()) / 304.8;
				double num2 = double.Parse(this.dataGridView1[1, this.dataGridView1.CurrentRow.Index].Value.ToString()) / 304.8;
				xmlDocument.Save(this.filePath);
				this.BindData();
			}
		}

		private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar != '-')
			{
				e.Handled = true;
			}
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || this.dataGridView1.CurrentCell.ColumnIndex == 1)
			{
				e.Control.KeyPress += this.TextBox_KeyPress;
			}
		}

		private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			Regex regex = new Regex("^[+-]?[0-9]+\\d*[.]?\\d*$");
			if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
			{
				if (!regex.IsMatch(e.FormattedValue.ToString().Trim()))
				{
					YJKMessageBox.Show("请输入有效数字！");
					e.Cancel = true;
					this.dataGridView1.CancelEdit();
					return;
				}
				e.Cancel = false;
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 2 && e.RowIndex >= 0)
			{
				ColorDialog colorDialog = new ColorDialog();
				colorDialog.AllowFullOpen = true;
				colorDialog.FullOpen = true;
                System.Drawing.Color xmldataColor = PipeShowHelper.getXMLDataColor(this.dataGridView1[2, e.RowIndex].Value.ToString());
				if (!xmldataColor.IsKnownColor)
				{
					int[] array = new int[16];
					array[0] = PipeShowHelper.GetIntAboutColor(xmldataColor);
					colorDialog.CustomColors = array;
				}
				colorDialog.Color = xmldataColor;
				DialogResult dialogResult = colorDialog.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					string newRGB = PipeShowHelper.getNewRGB(colorDialog.Color.R);
					string newRGB2 = PipeShowHelper.getNewRGB(colorDialog.Color.G);
					string newRGB3 = PipeShowHelper.getNewRGB(colorDialog.Color.B);
					string str = string.Concat(new string[]
					{
						"RGB ",
						newRGB.ToString(),
						" ",
						newRGB2.ToString(),
						" ",
						newRGB3.ToString()
					});
					this.dataGridView1[2, e.RowIndex].Value = "█" + str;
					this.dataGridView1[2, e.RowIndex].Style.ForeColor = colorDialog.Color;
					this.dataGridView1[2, e.RowIndex].ReadOnly = true;
				}
			}
		}

		private void chkElectric_CheckedChanged(object sender, EventArgs e)
		{
			this.chkDiameter.Enabled = this.chkElectric.Checked;
			this.hyTxtDiameter.Enabled = this.chkElectric.Checked;
		}

		private void ReadUserSetting()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.filePath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("elevationSet");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("isElectric");
			if (xmlNode2 != null)
			{
				this.chkElectric.Checked = (xmlNode2.InnerText == "true");
			}
			XmlNode xmlNode3 = xmlNode.SelectSingleNode("Range");
			if (xmlNode3 != null)
			{
				this.chkDiameter.Checked = (xmlNode3.InnerText == "true");
			}
			XmlNode xmlNode4 = xmlNode.SelectSingleNode("Diameter");
			if (xmlNode4 != null)
			{
				this.hyTxtDiameter.Text = xmlNode4.InnerText;
			}
		}

		private void WriteUserSetting()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.filePath);
			this.GetUserElement(xmlDocument, "isElectric", this.chkElectric.Checked ? "true" : "false");
			this.GetUserElement(xmlDocument, "Range", this.chkDiameter.Checked ? "true" : "false");
			this.GetUserElement(xmlDocument, "Diameter", this.hyTxtDiameter.Text);
			xmlDocument.Save(this.filePath);
		}

		private void GetUserElement(XmlDocument xmlDoc, string nodeName, string innerText)
		{
			if (innerText == null)
			{
				innerText = "0";
			}
			XmlNode xmlNode = xmlDoc.SelectSingleNode("elevationSet");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode(nodeName);
			if (xmlNode2 == null)
			{
				XmlElement xmlElement = xmlDoc.CreateElement(nodeName);
				xmlNode.AppendChild(xmlElement);
				xmlElement.InnerText = innerText;
				return;
			}
			xmlNode2.InnerText = innerText;
		}

		private DataSet ds;

		private readonly string filePath = "";

		private Document doc;

		public double dDiameter;

		public bool isElectric;
	}
}
